package com.mingtu.jgspatrol.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.ActivityUtil;
import com.mingtu.common.utils.ScreenUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.bean.ParkListBean;
import com.mingtu.jgspatrol.databinding.LayoutParkCommonTopSelectViewBinding;
import com.mingtu.jgspatrol.utils.MyConstant;

import java.util.List;

/**
 * @ClassName :
 * @Author : wkk
 * @Date : 2023-10-9 10:01:39
 * @Email : 1281573890@qq.com
 * @Eescription :
 */
public class ParkFilterView extends FrameLayout {
    private Activity activity;
    private Context context;
    private OnSelectResultListener OnSelectResultListener = null;
    private int checkPosition;
    private List<ParkListBean.ListBean> dataBeans;
    private String[] dateArray;
    private LayoutParkCommonTopSelectViewBinding binding;
    private String parkCode;

    public ParkFilterView(@NonNull Context context) {
        this(context, (AttributeSet) null, 0);
    }

    public ParkFilterView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ParkFilterView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        view = LayoutInflater.from(context).inflate(R.layout.layout_park_common_top_select_view, this, true);
        binding = LayoutParkCommonTopSelectViewBinding.inflate(LayoutInflater.from(context), this, true);
        this.activity = (Activity) context;
        this.context = context;
        getParkListData();
        binding.layoutSelect.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dateArray != null && dateArray.length > 0) {
                    showPop();
                } else {
                    ToastUtils.showLong("停车场列表数据获取失败！");
                }
            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        binding = null;
    }

    public void setOnSelectResultListener(OnSelectResultListener OnSelectResultListener) {
        this.OnSelectResultListener = OnSelectResultListener;
    }

    public static interface OnSelectResultListener {
        void onResult(String parkCode);
    }

    /**
     * 停车场列表
     */
    private void getParkListData() {
        OkGo.<String>post(MyConstant.POST_PARK_LIST)
                .tag(this)
                .upJson("")
                .execute(new StringDialogCallback(ActivityUtils.getTopActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            ParkListBean parkListBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                parkListBean = new ParkListBean();
                                parkListBean = gson.fromJson(result, ParkListBean.class);
                                dataBeans = parkListBean.getList();
                                dateArray = new String[dataBeans.size()];
                                for (int i = 0; i < dataBeans.size(); i++) {
                                    dateArray[i] = dataBeans.get(i).getName();
                                }
                                binding.tvText.setText(dateArray[0]);
                                parkCode = dataBeans.get(0).getParkCode();
                                if (OnSelectResultListener != null)
                                    OnSelectResultListener.onResult(parkCode);
                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                                ToastUtils.showLong(getResources().getString(R.string.text_json_error));
                            }
                        }
                    }
                });

    }

    /**
     * 显示停车场列表
     */
    private void showPop() {
        new XPopup.Builder(ActivityUtil.getTopActivity())
                .hasShadowBg(true)
                .maxHeight(ScreenUtils.getScreenHeight() / 2)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .asCenterList("请选择停车场", dateArray,
                        null, checkPosition,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                checkPosition = position;
                                binding.tvText.setText(dateArray[position]);
                                parkCode = dataBeans.get(position).getParkCode();
                                if (parkCode != null) {
                                    if (OnSelectResultListener != null)
                                        OnSelectResultListener.onResult(parkCode);
                                } else {
                                    ToastUtils.showLong("暂不支持此停车场！");
                                }
                            }
                        }).show();

    }

    public String getParkCode() {
        return parkCode;
    }

}
